package com.jiang.mybatis.controller;

import com.jiang.core.common.MSG;
import com.jiang.core.common.PageHelper;
import com.jiang.mybatis.service.UserService;
import com.jiang.mybatis.view.dto.UpdateGenderDTO;
import com.jiang.mybatis.view.dto.UserDTO;
import com.jiang.mybatis.view.dto.UserSearchDTO;
import com.jiang.mybatis.view.vo.UserVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;


@Api(tags = "UserController")
@RequestMapping("/v1/user")
@RestController
public class UserController {

    @Autowired
    private UserService userService;

    @ApiOperation(value = "新增")
    @PostMapping("/add")
    public MSG<?> add(@RequestBody @Valid UserDTO DTO){
        userService.add(DTO);
        return MSG.SUCCESS();
    }

    @ApiOperation(value = "删除")
    @DeleteMapping("/{id}")
    public MSG<?> del(@PathVariable("id") Long id){
        userService.del(id);
        return MSG.SUCCESS();
    }

    @ApiOperation(value = "更新")
    @PutMapping("/{id}")
    public MSG<?> update(@PathVariable("id") Long id, @RequestBody UserDTO DTO){
        userService.update(id, DTO);
        return MSG.SUCCESS();
    }

    /*@ApiOperation(value = "修改性别")
    @PatchMapping("/gender")
    public MSG<?> updateGender(@RequestBody UpdateGenderDTO DTO){
        userService.updateGender(DTO);
        return MSG.SUCCESS();
    }*/

    @ApiOperation(value = "主键查询")
    @GetMapping("/{id}")
    public MSG<UserVO> getById(@PathVariable("id") Long id){
        UserVO vo = userService.getById(id);
        return MSG.SUCCESS(vo);
    }

    @ApiOperation(value = "分页查询")
    @PostMapping("/page")
    public MSG<PageHelper<UserVO>> page(@RequestBody UserSearchDTO DTO){
        PageHelper<UserVO> page = userService.page(DTO);
        return MSG.SUCCESS(page);
    }
}